<?php

/**
 * @file
 * Renders administrative pages for the Responsive Images module
 */

function responsive_images_admin_settings() {
  $form = array();
  $image_styles = image_style_options(FALSE);

  $form['responsive_images_widthBreakPoint'] = array(
    '#type' => 'textfield',
    '#title' => t('widthBreakPoint'),
    '#description' => t('Specify a width breadpoint if you want to use a different one than the 480px default') . '<br />' . t('Leave blank to use the default settings.'),
    '#required' => FALSE,
    '#default_value' => variable_get('responsive_images_widthBreakPoint', ''),
    '#field_suffix' => ' ' . t('pixels'),
    '#size' => 6,
  );

  $form['responsive_images_small_image_style'] = array(
    '#type' => 'select',
    '#title' => t('Small image style'),
    '#description' => t('Select the image style to be used for responsive images in a small width context.'),
    '#required' => TRUE,
    '#multiple' => FALSE,
    '#options' => $image_styles,
    '#default_value' => variable_get('responsive_images_small_image_style', 'medium'),
  );

  $form['responsive_images_large_image_style'] = array(
    '#type' => 'select',
    '#title' => t('Large image style'),
    '#description' => t('Select the image style to be used for responsive images in a large width context.'),
    '#required' => TRUE,
    '#multiple' => FALSE,
    '#options' => $image_styles,
    '#default_value' => variable_get('responsive_images_large_image_style', 'large'),
  );

  $form['#submit'][] = 'responsive_images_admin_settings_redirect';

  return system_settings_form($form);
}

/**
 * Submit handler for responsive_images_admin_settings().
 * Adds a notification to warn user about possible change in .htaccess file
 * and redirects him/her to the instructions page
 */
function responsive_images_admin_settings_redirect($form, &$form_state) {
  drupal_set_message(t('If you have changed the image styles configuration, make sure to update your .htaccess file to reflect these changes.'), 'warning');
  $form_state['redirect'] = 'admin/config/media/responsive_images/instructions';
}

/**
 * Instructions page, assisting the user in configuring the .htaccess file
 */
function responsive_images_admin_instructions() {
  $form = array();
  $styles = array(
    'small' => variable_get('responsive_images_small_image_style', 'medium'),
    'large' => variable_get('responsive_images_large_image_style', 'large'),
  );

  $form['config_instructions_title'] = array(
    '#type' => 'item',
    '#markup' => '<h2>' . t('Configuration instructions') . '</h2>',
  );

  $library_path = responsive_images_get_library_file();
  if (!file_exists($library_path)) {
    $error = drupal_strtoupper(t('Warning')) . ': ' . t('Library is not correctly installed');
    $error .= '<br />' . t('Please follow the installation instructions in the responsive_images module README.txt file');
    $form['library'] = array(
      '#type' => 'item',
      '#title' => t('Responsive Images library'),
      '#markup' => $error,
    );
  }
  else {
    $form['library'] = array(
      '#type' => 'item',
      '#title' => t('Responsive Images library'),
      '#markup' => t('Library is installed.'),
    );

    $form['instructions'] = array(
      '#type' => 'item',
      '#title' => t('Follow these instructions to correctly update your .htaccess file'),
      '#markup' => 'Put the following code in your .htaccess file right after the "# RewriteBase /" directive.',
    );

    $form['htaccess_file'] = array(
      '#type' => 'fieldset',
      '#title' => t('.htaccess file example'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    $suffix_code = <<<SUFFIX
  # Pass all requests not referring directly to files in the filesystem to<br />
  # index.php. Clean URLs are handled in drupal_environment_initialize().<br />
  RewriteCond %{REQUEST_FILENAME} !-f<br />
  RewriteCond %{REQUEST_FILENAME} !-d<br />
  RewriteCond %{REQUEST_URI} !=/favicon.ico<br />
  RewriteRule ^ index.php [L]
SUFFIX;
    $form['htaccess_file']['htaccess_code'] = array(
      '#type' => 'textarea',
      '#required' => FALSE,
      '#rows' => 15,
      '#default_value' => responsive_images_admin_htaccess_code($styles),
      '#prefix' => '# RewriteBase /',
      '#suffix' => $suffix_code,
    );
  }

  return $form;
}

/**
 * Renders the .htaccess code to be used
 */
function responsive_images_admin_htaccess_code($styles) {
  $small_style = $styles['small'];
  $large_style = $styles['large'];

  $code = <<<CODE
    # ----------------------------------------------------------------------
    # Responsive Images
    # ----------------------------------------------------------------------
    # Mobile-First images that scale responsively and responsibly
    # Copyright 2011, Scott Jehl, Filament Group, Inc
    # MIT License
    # Modified for usage with Responsive Images Drupal module

    # -----------------------------------------
    # Start Responsive Images
    # -----------------------------------------
    RewriteCond %{HTTP_COOKIE} rwdimgsize=large

    # -----------------------------------------
    # Approach A: Immediate redirection
    # -----------------------------------------
    RewriteRule (.*)\/{$small_style}\/(.*)\.r(\.(jpe?g|png|gif))$ $1/{$large_style}/$2$3

    # -----------------------------------------
    # The following line applies to both approaches
    # -----------------------------------------
    # remove .r from all requests
    RewriteRule (.*)\.r(\.(jpe?g|png|gif))$ $1$2

    # -----------------------------------------
    # END Responsive Images
    # -----------------------------------------
CODE;
  return $code;
}
